<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
    <th:block th:include="include :: header('订单列表列表')" />
    <th:block th:include="include :: select2-css" />
    <th:block th:include="include :: s2ztree-css" />
    <style type="text/css">
        .select-table .table td {
            /* 超出部分隐藏 */
            /*overflow:hidden;*/
            /*!* 超出部分显示省略号 *!*/
            /*text-overflow:ellipsis;*/
            /*规定段落中的文本不进行换行 */
            white-space:nowrap;
            /* 配合宽度来使用 */
            height:40px;
        }
    </style>
</head>
<body class="gray-bg">
     <div class="container-div">
        <div class="row">
            <div class="col-sm-12 search-collapse">
                <form id="formId">
                    <div class="select-list">
                        <ul>
                            <li th:if="${isShow}">
                                <label>归属码商：</label>
                                <!--                                <select name="recipientUid" id="recipientUid" class="form-control"-->
                                <!--                                        th:with="type=${@sysUser.selectUserListByType('ms')}">-->
                                <!--                                    <option value="">所有</option>-->
                                <!--                                    <option th:each="dict : ${type}" th:text="${dict.userName}"-->
                                <!--                                            th:value="${dict.userId}"></option>-->
                                <!--                                </select>-->
                                <select  name="recipientUid" id="recipientUid" class="form-control"></select>

                            </li>
                            <li th:if="${isShow}">
                                <label>商户名称：</label>
                                <select name="userId" id="userId" class="form-control"
                                        th:with="type=${@sysUser.selectUserListByType('sh')}">
                                    <option value="">所有</option>
                                    <option th:each="dict : ${type}" th:text="${dict.userName}"
                                            th:value="${dict.userId}"></option>
                                </select>
                            </li>
                            <li>
                                <label>通道类型：</label>
                                <select name="channelType" id="channelType"  class="form-control" th:with="type=${@dict.getType('channel_type')}" onchange="changeType()">
                                    <option value="">所有</option>
                                    <option th:each="dict : ${type}" th:text="${dict.dictLabel}" th:value="${dict.dictValue}"></option>
                                </select>
                            </li>
                            <li>
                                <label>通道编码：</label>
                                <select name="channel" id="channel"  class="form-control" >
                                    <option value="">所有</option>
                                </select>
                            </li>
                            <li>
                                <label>通道账号：</label>
                                <input type="text" name="channelAccount"/>
                            </li>

                            <li>
                                <label>商户单号：</label>
                                <input type="text" name="outTradeNo"/>
                            </li>
                            <li>
                                <p>开始时间：</p>
                                <!-- th:value="${#dates.format(new java.util.Date(), 'yyyy-MM-dd')}"-->
                                <!--                            <input type="text" class="time-input" id="startTime" placeholder="开始时间"-->
                                <!--                                   th:value="${#dates.format(new java.util.Date(), 'yyyy-MM-dd HH:mm:ss')}"-->
                                <!--                                   data-type="datetime"-->
                                <!--                                   data-format="yyyy-MM-dd HH:mm:ss"-->
                                <!--                                   name="params[beginCreateTime]"/>-->
                                <input type="text" class="form-control" id="startTime"  name="params[beginCreateTime]"
                                       th:value="${#dates.format(new java.util.Date(), 'yyyy-MM-dd 00:00:00')}"
                                       placeholder="yyyy-MM-dd HH:mm:ss">

                                <!--                            <input type="text" class="time-input" id="endTime" placeholder="结束时间"-->
                                <!--                                   th:value="${#dates.format(new java.util.Date(), 'yyyy-MM-dd HH:mm:ss')}"-->
                                <!--                                   data-type="datetime"-->
                                <!--                                   data-format="yyyy-MM-dd HH:mm:ss"-->
                                <!--                                   name="params[endCreateTime]"/>-->
                            </li>
                            <li>
                                <p>结束时间：</p>
                                <!-- th:value="${#dates.format(new java.util.Date(), 'yyyy-MM-dd')}"-->
                                <!--                            <input type="text" class="time-input" id="startTime" placeholder="开始时间"-->
                                <!--                                   th:value="${#dates.format(new java.util.Date(), 'yyyy-MM-dd HH:mm:ss')}"-->
                                <!--                                   data-type="datetime"-->
                                <!--                                   data-format="yyyy-MM-dd HH:mm:ss"-->
                                <!--                                   name="params[beginCreateTime]"/>-->

                                <input type="text" class="form-control" id="endTime"  name="params[endCreateTime]"
                                       th:value="${#dates.format(new java.util.Date(), 'yyyy-MM-dd 23:59:59')}"
                                       placeholder="yyyy-MM-dd HH:mm:ss">
                                <!--                            <input type="text" class="time-input" id="endTime" placeholder="结束时间"-->
                                <!--                                   th:value="${#dates.format(new java.util.Date(), 'yyyy-MM-dd HH:mm:ss')}"-->
                                <!--                                   data-type="datetime"-->
                                <!--                                   data-format="yyyy-MM-dd HH:mm:ss"-->
                                <!--                                   name="params[endCreateTime]"/>-->
                            </li>
                            <li>
                                <label>订单状态：</label>
                                <select name="status"  class="form-control" th:with="type=${@dict.getType('order_status')}">
                                    <option value="">所有</option>
                                    <option th:each="dict : ${type}" th:text="${dict.dictLabel}" th:value="${dict.dictValue}"></option>
                                </select>
                            </li>
                            <li>
                                <label>三方单号：</label>
                                <input type="text" name="pay3rdOrderNo"/>
                            </li>
<!--                            <li>-->
<!--                                <label>通知状态：</label>-->
<!--                                <input type="text" name="notifySucc"/>-->
<!--                            </li>-->
                            <li>
                                <label>人工补单：</label>
<!--                                <input type="text" name="manualSucc"/>-->
                                <select name="manualSucc"  class="form-control" th:with="type=${@dict.getType('yes_or_no')}">
                                    <option value="">所有</option>
                                    <option th:each="dict : ${type}" th:text="${dict.dictLabel}" th:value="${dict.dictValue}"></option>
                                </select>
                            </li>
                            <li>
                                <label>盘口单号：</label>
                                <input type="text" name="customOrderNo"/>
                            </li>
                            <li>
                                <a class="btn btn-primary btn-rounded btn-sm" onclick="queryAll()"><i class="fa fa-search"></i>&nbsp;搜索</a>
                                <a class="btn btn-warning btn-rounded btn-sm" onclick="$.form.reset()"><i class="fa fa-refresh"></i>&nbsp;重置</a>
                            </li>
                        </ul>
                    </div>
                </form>
            </div>

<!--            <div class="btn-group-sm" id="toolbar" role="group">-->
<!--                <a class="btn btn-success" onclick="$.operate.add()" shiro:hasPermission="business:orderInfo:add">-->
<!--                    <i class="fa fa-plus"></i> 添加-->
<!--                </a>-->
<!--                <a class="btn btn-primary single disabled" onclick="$.operate.edit()" shiro:hasPermission="business:orderInfo:edit">-->
<!--                    <i class="fa fa-edit"></i> 修改-->
<!--                </a>-->
<!--                <a class="btn btn-danger multiple disabled" onclick="$.operate.removeAll()" shiro:hasPermission="business:orderInfo:remove">-->
<!--                    <i class="fa fa-remove"></i> 删除-->
<!--                </a>-->
<!--                <a class="btn btn-warning" onclick="$.table.exportExcel()" shiro:hasPermission="business:orderInfo:export">-->
<!--                    <i class="fa fa-download"></i> 导出-->
<!--                </a>-->
<!--            </div>-->
            <div class="btn-group-sm" id="toolbar" role="group">
                <!--<a class="btn btn-success" onclick="$.operate.add()" shiro:hasPermission="clearing:clearingOrder:add">
                    <i class="fa fa-plus"></i> 添加
                </a>
                <a class="btn btn-primary single disabled" onclick="$.operate.edit()"
                   shiro:hasPermission="clearing:clearingOrder:edit">
                    <i class="fa fa-edit"></i> 修改
                </a>
                <a class="btn btn-danger multiple disabled" onclick="$.operate.removeAll()"
                   shiro:hasPermission="clearing:clearingOrder:remove">
                    <i class="fa fa-remove"></i> 删除
                </a>-->
                <a class="btn btn-warning" onclick="$.table.exportExcel()"
                   shiro:hasPermission="clearing:clearingOrder:export">
                    <i class="fa fa-download"></i> 导出
                </a>
<!--                实时总收款：<span class="badge badge-warning" id="total_amount_span"></span>-->
<!--                实时已收款：<span class="badge badge-primary" id="succ_amount_span"></span>-->
<!--                总笔数：<span class="badge badge-warning" id="total_count_span"></span>-->
<!--                成功笔数：<span class="badge badge-primary" id="succ_count_span"></span>-->
<!--                实时成功率：<span class="badge badge-danger" id="cgl_span"></span>-->
                总下单金额：<span class="badge badge-warning" id="stotalMoney">0</span>
                成功总笔数：<span class="badge badge-primary" id="totalCount">0</span>
                成功总金额：<span class="badge badge-primary" id="stotalReceiptMoney">0</span>
                成功率：<span class="badge badge-danger" id="totalCgl">0%</span>
            </div>
            <div class="col-sm-12 select-table table-striped">
                <table id="bootstrap-table"></table>
            </div>
        </div>
    </div>
    <th:block th:include="include :: footer" />
     <th:block th:include="include :: select2-js" />
     <th:block th:include="include :: s2ztree-js" />
    <script th:inline="javascript">
        var setting = {
            view : {
                dblClickExpand : false,
                expandAll: true  // 设置默认展开所有节点
            },
            data : {
                simpleData : {
                    enable : true
                }
            }
        };
        var userTreeurl = ctx + "system/user/userTreeData" ;
        var editFlag = [[${@permission.hasPermi('business:orderInfo:edit')}]];
        var removeFlag = [[${@permission.hasPermi('business:orderInfo:remove')}]];
        var reissueFlag = [[${@permission.hasPermi('business:orderInfo:reissue')}]];

        var prefix = ctx + "business/orderInfo";
        var orderStatusDatas = [[${@dict.getType('order_status')}]];
        var yesnoDatas = [[${@dict.getType('yes_or_no')}]];

        $(function() {
            getUserSelectTree();

            layui.use('laydate', function(){
                var laydate = layui.laydate;
                laydate.render({
                    elem: '#startTime',
                    type: 'datetime',
                    trigger: 'click'
                });
                laydate.render({
                    elem: '#endTime',
                    type: 'datetime',
                    trigger: 'click'
                });
            });
            var options = {
                url: prefix + "/list",
                createUrl: prefix + "/add",
                updateUrl: prefix + "/edit/{id}",
                removeUrl: prefix + "/remove",
                exportUrl: prefix + "/export",
                modalName: "订单列表",
                detailView: true,
                onExpandRow : function(index, row, $detail) {
                    initChildTable(index, row, $detail);
                },
                columns: [{
                    checkbox: true
                },
                {
                    field: 'id',
                    title: '平台订单号'
                },
                {
                    field: 'userName',
                    title: '商户名称'
                },
                {
                    field: 'channelDesc',
                    title: '通道名称'
                },
                {
                    field: 'channelType',
                    title: '通道类型'
                },
                {
                    field: 'channelAccount',
                    title: '通道账号',
                    formatter: function (value, row, index) {
                        if(row.recipientRealName!=null){
                            return row.recipientRealName;
                        }else{
                            return row.channelAccount;
                        }
                    }
                },

                {
                    field: 'recipientUName',
                    title: '归属码商'
                },
                {
                    field: 'aliAuthCode',
                    title: '付款人姓名'
                },
                {
                    field: 'money',
                    title: '订单金额',
                    formatter: function (value, row, index) {
                        return (row.money == null ? '-' : row.money/100 + '元')
                    }
                },
                {
                    field: 'realMoney',
                    title: '实际金额',
                    formatter: function (value, row, index) {
                        return (row.realMoney == null ? '-' : row.realMoney/100 + '元')
                    }
                },
                // {
                //     field: 'qrcodeContent',
                //     title: '机器名称'
                // },
                // {
                //     field: 'goodsName',
                //     title: '货道',
                // },
                {
                    field: 'outTradeNo',
                    title: '商户订单号'
                },
                {
                    field: 'createTime',
                    title: '创建时间'
                },
                {
                    field: 'status',
                    title: '订单状态',
                    formatter: function (value, row, index) {
                        if (row.status == '2' ) {
                            return '<span style="color: green">' + $.table.selectDictLabel(orderStatusDatas, value) + '</span>';
                        } else {
                            return '<span style="color: red">' + $.table.selectDictLabel(orderStatusDatas, value) + '</span>';
                        }
                    },
                },
                {
                    field: 'pay3rdOrderNo',
                    title: '三方单号'
                },
                {
                    field: 'notifySuccTime',
                    title: '回调时间'
                },

                {
                    field: 'manualSucc',
                    title: '是否人工补单',
                    formatter: function (value, row, index) {
                        if (row.status == '2' ) {
                            return '<span style="color: green">' + $.table.selectDictLabel(yesnoDatas, value) + '</span>';
                        } else {
                            return '<span style="color: red">' + $.table.selectDictLabel(yesnoDatas, value) + '</span>';
                        }
                    },
                },
                // {
                //     field: 'payurlLastseeIp',
                //     title: '最近访问支付地址的ip'
                // },

                {
                    field: 'customOrderNo',
                    title: '盘口订单号'
                },
                {
                    title: '操作',
                    align: 'center',
                    formatter: function(value, row, index) {
                        var actions = [];
                        // actions.push('<a class="btn btn-success btn-xs ' + editFlag + '" href="javascript:void(0)" onclick="$.operate.edit(\'' + row.id + '\')"><i class="fa fa-edit"></i>编辑</a> ');
                        // actions.push('<a class="btn btn-danger btn-xs ' + removeFlag + '" href="javascript:void(0)" onclick="$.operate.remove(\'' + row.id + '\')"><i class="fa fa-remove"></i>删除</a>');
                        if(row.status!='2'){
                            actions.push('<a class="btn btn-danger btn-xs ' + reissueFlag + '" href="javascript:void(0)" onclick="smartReissueOrder(\'' + row.id + '\')"><i class="fa fa-remove"></i>智能补单</a> ');
                            actions.push('<a class="btn btn-danger btn-xs ' + reissueFlag + '" href="javascript:void(0)" onclick="reissueOrder(\'' + row.id + '\')"><i class="fa fa-remove"></i>强补</a>');

                        }
                        return actions.join('');
                    }
                }],
                rowStyle: function rowStyle(row, index) {
                    if (row.status == '2') {
                        return {
                            css: {"color": "green"}
                        };
                    } else {
                        return {
                            css: {"color": "red"}
                        };
                    }
                }
            };
            $.table.init(options);
        });

        function reissueOrder(id) {

            layer.prompt({
                formType: 0,
                title: '请输入谷歌秘钥',
            }, function (value, index, elem) {
                var reg = /^-?\d+$/;
                if (!reg.test(value)) {
                    $.modal.msgError("请输入正确谷歌验证码");
                    return;
                }
                layer.close(index);
                var data = {"id": id, googleCode: value};
                var config = {
                    url: prefix + "/reissueOrder",
                    type: "post",
                    dataType: "json",
                    data: data,
                    beforeSend: function () {
                        $.modal.loading("正在处理中，请稍后...");
                        // $.modal.disable();
                    },
                    success: function (result) {
                        if (result.code == web_status.SUCCESS) {
                            $.modal.msgSuccess(result.msg);
                        } else {
                            $.modal.msgError(result.msg);
                        }
                        $.modal.closeLoading();
                        $.table.refresh();
                    }
                };
                $.ajax(config)
                // $.operate.post(prefix + "/reissueOrder", data);

            });

        }
        function smartReissueOrder(id){
            var data = {"id": id};
            var config = {
                url: prefix + "/smartReissueOrder",
                type: "post",
                dataType: "json",
                data: data,
                beforeSend: function () {
                    $.modal.loading("正在处理中，请稍后...");
                    // $.modal.disable();
                },
                success: function (result) {
                    if (result.code == web_status.SUCCESS) {
                        $.modal.msgSuccess(result.msg);
                    } else {
                        $.modal.alertError(result.msg);
                    }
                    $.modal.closeLoading();
                    $.table.refresh();
                }
            };
            $.ajax(config)
            // $.operate.post(prefix + "/smartReissueOrder", data);

        }
        function changeType(){
            if($("#channelType").val()==""){
                $("#channel").html("<option value=\"\">所有</option>");
                return;
            }
            requrl=ctx+"business/channel/getChannel/"+$("#channelType").val();
            var config = {
                url:requrl ,
                type: "post",
                dataType: "json",
                beforeSend: function () {
                    $.modal.loading("正在处理中，请稍后...");
                    // $.modal.disable();
                },
                success: function(result) {
                    $.modal.closeLoading();
                    if (result.code == web_status.SUCCESS) {
                        $("#channel").html("<option value=\"\">所有</option>");
                        $.each(result.data, function(key, val) {
                            var option1 = $("<option>").val(val.channel).text(val.channelDesc);
                            $("#channel").append(option1);
                            // form.render('select');
                        });
                    }else{
                        $.modal.alertError(result.msg);
                    }
                }
            };
            $.ajax(config)
        }
        queryTotal();
        function queryTotal() {
            var currentId = $('form').attr('id');
            var params = $.common.formToJSON(currentId);
            var config = {
                url: prefix + "/statistic",
                type: "post",
                dataType: "json",
                data: params,
                beforeSend: function () {
                    // $.modal.loading("正在处理中，请稍后...");
                    // $.modal.disable();
                },
                success: function (result) {
                    $("#stotalMoney").html(result.data.totalMoney/100+"元");
                    $("#totalCount").html(result.data.totalCount+"笔");
                    $("#stotalReceiptMoney").html(result.data.totalReceiptMoney/100+"元");
                    $("#totalCgl").html(result.data.totalCgl+"%");
                    // $.modal.closeLoading();
                }
            };
            $.ajax(config)
        }

        initChildTable = function(index, row, $detail) {
            var childTable = $detail.html('<table style="table-layout:fixed"></table>').find('table');
            if (row.channel == 'isv_alipay_zft') {
                $(childTable).bootstrapTable({
                    url: ctx + "business/mergeOrderDetail/getMergeOrderDetailList/" + row.id,
                    method: 'post',
                    sidePagination: "server",
                    contentType: "application/json",
                    queryParams: {},
                    columns: [
                        {
                            field: 'orderId',
                            title: '系统订单编码'
                        },
                        {
                            field: 'mergeNo',
                            title: '支付宝合并单号'
                        },
                        {
                            field: 'outTradeNo',
                            title: '支付宝商户订单号'
                        },
                        {
                            field: 'subMerName',
                            title: '二级商户名称'
                        },
                        {
                            field: 'status',
                            title: '订单状态',
                            align: 'center'
                        }],
                    rowStyle: function rowStyle(row, index) {
                        if (row.status == 'TRADE_SUCCESS' || row.status == 'TRADE_FINISHED') {
                            return {
                                css: {"color": "green"}
                            };
                        } else {
                            return {
                                css: {"color": "red"}
                            };
                        }
                    }
                });
            }else if (row.channel.indexOf("smj_") !=-1 ) {
                Data = [
                    {machineName: row.qrcodeContent,hd: row.goodsName,msgPayResult: row.msgPayResult}
                ];
                childTable.bootstrapTable({
                    data : Data,
                    //toolbar: '#toolBar',
                    cache: false, //是否使用缓存，默认为true，
                    columns: [
                        {field : 'machineName', title: '机器名称',  align:"center"},
                        {field : 'hd', title: '货道', halign:"center",align:"center"},
                        {field : 'msgPayResult', title: '渠道回调信息',  align:"center"},
                    ],
                    formatNoMatches : function() {
                        return '<div style="color: #b8b8b8; height: 15px;"> 暂无数据 </div>';// 如果无数据则显示这个
                    }
                });

            }else {
                Data = [
                    {msgPayResult: row.msgPayResult}
                ];
                childTable.bootstrapTable({
                    data : Data,
                    //toolbar: '#toolBar',
                    cache: false, //是否使用缓存，默认为true，
                    columns: [
                        {field : 'msgPayResult', title: '渠道回调信息',  align:"center"},
                    ],
                    formatNoMatches : function() {
                        return '<div style="color: #b8b8b8; height: 15px;"> 暂无数据 </div>';// 如果无数据则显示这个
                    }
                });

            }
        };
        function queryAll(){
            $.table.search();
            queryTotal();
        }
        function getUserSelectTree() {
            //查询条件
            var params = {
                merchantType: "ms"
            };
            var config = {
                url:userTreeurl ,
                type: "post",
                dataType: "json",
                data: params,
                beforeSend: function () {
                    $.modal.loading("正在处理中，请稍后...");
                    // $.modal.disable();
                },
                success: function(result) {
                    $.modal.closeLoading();
                    $('select[name=recipientUid]').select2ztree({
                        textField : 'name',
                        titleField : 'name',
                        ztree : {
                            setting : $.extend(true, {
                                treeId : 'userId'
                            }, setting),
                            zNodes : result
                        }
                    });

                }
            };
            $.ajax(config)
        }
    </script>
</body>
</html>